//
//  MZTextFieldTableViewCell.m
//  meizhuang
//
//  Created by hzduanjiashun on 16/7/13.
//  Copyright © 2016年 netease. All rights reserved.
//
#import <Masonry/Masonry.h>

#import "WFTextFieldTableViewCell.h"
#import "UITextField+ClearButton.h"

@implementation WFTextFieldTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self setup];
    }
    return self;
}

- (instancetype)init
{
    self = [super init];
    if (self) {
        [self setup];
    }
    return self;
}

- (void)setup {
    _contentInset = UIEdgeInsetsMake(0, 15, 0, 0);
    self.titleWidth = 60;
    self.clipsToBounds = YES;
    
    _titleLabel = [UILabel new];
    _titleLabel.font = SCFont(14);
    [self.contentView addSubview:_titleLabel];
    
    _titleIcon = [UIImageView new];
    _titleIcon.contentMode = UIViewContentModeCenter;
    [self.contentView addSubview:_titleIcon];
    
    _textField = [UITextField new];
    _textField.font = SCFont(14);
    [_textField customizeClearButton];
    [self.contentView addSubview:_textField];
    
}

- (void)reloadConstraints {
    if (self.titleIcon) {
        [self.titleIcon mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.top.left.bottom.insets(self.contentInset);
        }];
    } else {
        [self.titleIcon mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.top.left.bottom.insets(self.contentInset);
            make.width.mas_equalTo(0);
        }];
    }
    [self.titleLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.left.bottom.insets(self.contentInset);
        make.width.mas_lessThanOrEqualTo(self.contentInset.left + self.titleWidth);
    }];
    [self.textField mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(self.contentInset.left + self.titleWidth);
        make.top.bottom.right.insets(self.contentInset);
    }];
}

- (void)layoutSubviews {
    [super layoutSubviews];
    [self reloadConstraints];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    if (selected) {
        [self.textField becomeFirstResponder];
    }
}


@end
